<?php

class PorderModel extends Model{
	
	public $table ='porder';
	
	public function add_data(){
		
		
		$sid=explode(',', Q('get.sid'));
//		p($sid);
//		p($_SESSION);
		
		$arr=$_SESSION['cart']['goods'];
		$all=array_keys($arr);
//		p($all);
		$a=array_diff($all,$sid);
		
//		p($a);
		$order=array();
		foreach ($sid as $k => $v) {
			foreach ($arr as $key => $value) {
				if($v==$key){
					$order[]=$value;
				}
			}
		}
		$data=array();
		$data['total']=0;
		$data['num']=0;
		foreach ($order as $k => $v) {
			$data['total'] +=$v['total'];
			$data['num'] +=$v['num'];
		}
		
//		添加订单表
		$arr=array(
			'consignee'=>12,
			'tprice'=>$data['total'],
			'consignee'=>Q('post.consignee'),
			'address'=>Q('post.address'),
			'otime'=>time(),
			'uid'=>$_SESSION['uid'],
			'orderno'=>Cart::getOrderId(),
		);
		$oid = $this->add($arr);
//		p($order);die;
		
		$temp=array();
		foreach ($order as $k => $v) {
			$temp[]=array(
				'pno'=>$v['num'],
				'sprice'=>$v['total'],
				'gid'=>$v['id'],
				'oid'=>$oid,
				'option'=>implode($v['options']),
			);
		}
		
//		添加订单列表
		foreach ($temp as $k => $v) {
			K('Olist')->add_data($v);
		}
//		删除购物车中的对应session
		foreach ($sid as $k => $v) {
			Cart::del($v);
		}
		
	}
	
	public function get_all_data($where=null){
		
		return $this->where($where)->all();
		
	}
	
	public function del_data($where=null){
		
		$oid = $this->where($where)->getField('oid');
		$this->where($where)->delete();
		return K('Olist')->del_data(array('oid'=>$oid));
	}
	
	
}




















